package com.example.inventorywarningdemo.util;

import com.example.inventorywarningdemo.config.AlertConfig;
import com.example.inventorywarningdemo.entity.Store;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

import java.time.LocalDate;

@Component
@RequiredArgsConstructor
public class MailUtil {
    private final JavaMailSender mailSender;
    private final AlertConfig alertConfig;
    
    @Value("${alert.to-email}")
    private String alertReceiver;

    public void sendAlertEmail(Store store) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setTo(alertReceiver);
        message.setSubject("店铺营业额预警通知");

        String content = String.format(
                "店铺名称：%s\n当日营业额：%.2f\n预警阈值：%.2f\n时间：%s",
                store.getStoreName(),
                store.getDailySales(),  // 使用动态计算的销售额
                store.getWarningThreshold(),
                LocalDate.now()
        );

        message.setText(content);
        mailSender.send(message);
    }
}